home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- /*------------------------------------------------------------------------------
- *
- * OORT - text.c - Routines for displaying and handling text.
- *
- * $Id: text.c,v 1.3 1994/01/28 00:21:46 mtj Exp $
- *
- * Chris Fouts - May, 1993.
- *
- *----------------------------------------------------------------------------*/
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <bstring.h>
- #include <fcntl.h>
- #include <gl.h>
- #include <Performer/pf.h>
-
- #include "text.h"
- #include "oort.h"
- #include "load.h"
-
- /* BEGIN PROTOTYPES -S text.c */
- static void drawLines( short *data ) ;
- static void drawMesh( short *data ) ;
- /* END PROTOTYPES -S text.c */
-
-
-
- static float xPos = 0.0f ;
- static float yPos = 0.0f ;
-
-
-
- /*------------------------------------------------------------------------------
- * Load a mesh or outline font.
- *----------------------------------------------------------------------------*/
- GeoFont *
- loadFont(
- char *name
- )
- {
- int fd ;
- long nChars ;
- long magic ;
- long datalen ;
- char *fileName ;
- GeoFont *gf ;
-
- gf = (GeoFont *)myMalloc( sizeof( GeoFont ) ) ;
-
- fileName = findFile( name ) ;
-
- if( gf == NULL )
- {
- return( NULL ) ;
- }
-
- if( ( fd = open( fileName, O_RDONLY ) ) < 0 )
- {
- perror( fileName ) ;
- exit( 1 ) ;
- }
-
- readIn( fd, &magic, sizeof( magic ) ) ;
-
- if( magic != OORT_FONT )
- {
- fprintf( stderr, "%s: bad magic number\n", fileName ) ;
- exit( 1 ) ;
- }
-
- readIn( fd, &(gf->type), sizeof( gf->type ) ) ;
- readIn( fd, &(gf->minChar), sizeof( gf->minChar ) ) ;
- readIn( fd, &(gf->maxChar), sizeof( gf->maxChar ) ) ;
- readIn( fd, &(gf->heightUp), sizeof( gf->heightUp ) ) ;
- readIn( fd, &(gf->heightLo), sizeof( gf->heightLo ) ) ;
- nChars = gf->maxChar - gf->minChar + 1 ;
-
- gf->widths = (short *)myMalloc( nChars * sizeof( short ) ) ;
- readIn( fd, gf->widths, nChars * sizeof( short ) ) ;
-
- gf->offsets = (short *)myMalloc( nChars * sizeof( short ) ) ;
- readIn( fd, gf->offsets, nChars * sizeof( short ) ) ;
-
- readIn( fd, &datalen, sizeof( datalen ) ) ;
-
- gf->data = (short *)myMalloc( datalen * sizeof( short ) ) ;
- readIn( fd, gf->data, datalen * sizeof( short ) ) ;
-
- close( fd ) ;
-
-
- if( gf->type == 1 )
- {
- gf->draw = drawMesh ;
- }
- else
- {
- gf->draw = drawLines ;
- }
-
- return( gf ) ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Draw a font string.
- *----------------------------------------------------------------------------*/
- void
- drawString(
- GeoFont *gf,
- char *str
- )
- {
- char c ;
- int i = 0 ;
-
- pfPushMatrix() ;
- translate( xPos, yPos, 0.0f ) ;
- scale( gf->scale, gf->scale, 1.0f ) ;
- translate( 0.0f, (float)-gf->heightLo, 0.0f ) ;
- while( ( c = str[i] ) )
- {
- if( gf->minChar <= c && c <= gf->maxChar )
- {
- c -= gf->minChar ;
- if( gf->offsets[c] >= 0 )
- {
- gf->draw( gf->data + gf->offsets[c] ) ;
- }
- translate( gf->widths[c], 0.0f, 0.0f ) ;
- }
- i++ ;
- }
- pfPopMatrix() ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Draw a tmeshed font string.
- *----------------------------------------------------------------------------*/
- static void
- drawMesh(
- short *data
- )
- {
- int i = 0 ;
- int npts ;
- short glyph ;
-
- if( *data != BGNTMESH )
- {
- fprintf( stderr, "drawMesh: bad start info (%hd)\n", *data ) ;
- return ;
- }
-
- while( ( glyph = data[i++] ) != RETENDTMESH )
- {
- switch( glyph )
- {
- case BGNTMESH :
- bgntmesh() ;
- break ;
- case SWAPTMESH :
- swaptmesh() ;
- break ;
- case ENDBGNTMESH :
- endtmesh() ;
- bgntmesh() ;
- break ;
- default :
- fprintf( stderr, "drawMesh: bad info (%hd)\n",
- glyph ) ;
- return ;
- }
-
- npts = data[i++] ;
- while( npts-- > 0 )
- {
- v2s( data + i ) ;
- i += 2 ;
- }
- }
- endtmesh() ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Draw an outline font string.
- *----------------------------------------------------------------------------*/
- static void
- drawLines(
- short *data
- )
- {
- int i = 0 ;
- int npts ;
- short glyph ;
-
- while( ( glyph = data[i++] ) != RETENDLOOP )
- {
- switch( glyph )
- {
- case BGNLOOP :
- bgnclosedline() ;
- break ;
- case ENDBGNLOOP :
- endclosedline() ;
- bgnclosedline() ;
- break ;
- case RETLOOP :
- endclosedline() ;
- return ;
- default :
- fprintf( stderr, "drawMesh: bad info (%hd)\n",
- glyph ) ;
- return ;
- }
-
- npts = data[i++] ;
- while( npts-- > 0 )
- {
- v2s( data + i ) ;
- i += 2 ;
- }
- }
- endclosedline() ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Set the scale factor of the font.
- *----------------------------------------------------------------------------*/
- void
- setFontSize(
- GeoFont *gf,
- float size
- )
- {
- gf->scale = size / (float)( gf->heightUp - gf->heightLo ) ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Get the font height in real world coordinates.
- *----------------------------------------------------------------------------*/
- float
- getFontSize(
- GeoFont *gf
- )
- {
- return( gf->scale * (float)( gf->heightUp - gf->heightLo ) ) ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Return the number of characters that will fit in a given width.
- *----------------------------------------------------------------------------*/
- int
- maxCharsInWidth(
- GeoFont *gf,
- char *str,
- float width
- )
- {
- int i = 0 ;
- float w = 0.0f ;
-
- while( str[i] )
- {
- if( gf->minChar <= str[i] && str[i] <= gf->maxChar )
- {
- w += gf->scale * gf->widths[str[i] - gf->minChar] ;
- if( w > width )
- {
- break ;
- }
- }
- i++ ;
- }
-
- return( i ) ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Return the width of a string.
- *----------------------------------------------------------------------------*/
- float
- getStrWidth(
- GeoFont *gf,
- char *str
- )
- {
- int i = 0 ;
- float w = 0.0f ;
-
- while( str[i] )
- {
- if( gf->minChar <= str[i] && str[i] <= gf->maxChar )
- {
- w += gf->widths[str[i] - gf->minChar] ;
- }
- i++ ;
- }
-
- return( w * gf->scale ) ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Move the text cursor.
- *----------------------------------------------------------------------------*/
- void
- positionText(
- float x,
- float y
- )
- {
- xPos = x ;
- yPos = y ;
- }
-
-
-
- /*------------------------------------------------------------------------------
- * Copy a font structure.
- *----------------------------------------------------------------------------*/
- GeoFont *
- copyFont(
- GeoFont *src
- )
- {
- GeoFont *dst ;
-
- dst = (GeoFont *)myMalloc( sizeof( GeoFont ) ) ;
-
- bcopy( src, dst, sizeof( GeoFont ) ) ;
-
- return( dst ) ;
- }
-